package org.aeon.client.view.components.sessionPanel;

import lombok.extern.slf4j.Slf4j;
import org.aeon.entity.dto.FriendApplyDto;
import org.aeon.entity.dto.UserDto;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.LinkedHashMap;

/**
 * @author: MissingAeon
 * @date: 2024/2/21 13:10
 * @description:
 */
@Slf4j
public class NotificationsPanel extends JPanel {
    private final JLabel titleL;
    private final JScrollPane scrollPane;
    private final GridBagConstraints gbc;

    public NotificationsPanel() {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        this.add(Box.createVerticalStrut(15));

        titleL = new JLabel();
        titleL.setFont(new Font("黑体", Font.PLAIN, 22));
        this.add(titleL);

        this.add(Box.createVerticalStrut(10));

        // 创建 JScrollPane
        scrollPane = new JScrollPane();
        Border border = BorderFactory.createMatteBorder(1, 0, 1, 0,
                new Color(0xA0CECECE, true));
        scrollPane.setBorder(border);
        // 设置 JScrollPane 的大小
        scrollPane.setPreferredSize(new Dimension(700, 700));

        // 将 JScrollPane 添加到 JFrame 中
        this.add(scrollPane);

        // 创建 GridBagConstraints 对象以控制组件布局
        gbc = new GridBagConstraints();
        // gbc.anchor = GridBagConstraints.NORTH;
        gbc.gridx = 0;
        // 使组件在垂直方向上相对于上一个组件定位
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        // 设置组件之间的间距
        gbc.insets = new Insets(10, 5, 5, 5);
    }

    private JPanel prepare() {
        // 创建一个面板来放置按钮
        final JPanel panel = new JPanel(new GridBagLayout());
        // panel.setBackground(Color.cyan);

        final Box box = Box.createVerticalBox();
        box.add(panel);
        box.add(Box.createVerticalGlue());

        scrollPane.setViewportView(box);

        return panel;
    }

    /**
     * 修改显示好友通知或群聊通知
     *
     * @param title
     */
    public void setTitle(String title) {
        titleL.setText(title);
    }


    public void fillInData(LinkedHashMap<FriendApplyDto, UserDto> map) {
        final JPanel panel = prepare();

        map.forEach((k, v) -> {
            // 将按钮添加到面板中
            final FriendNotificationsItem item = new FriendNotificationsItem(v, k);
            panel.add(item, gbc);
        });
        // final GroupNotificationsItem item = new GroupNotificationsItem(GroupNotificationType.INVITE_TO_JOIN,
        //         "用户名", "昵称", "2021/12/30 17:14:00", "群聊" + i);
        // panel.add(item, gbc);

        // 设置 panel 的大小
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, map.size() * 80));
    }

}
